#include "gdt.h"
#include "tss.h"


/************************
 *       GDT
 ************************
 * 
 * Los selectores se expresan asi: 
 * indice_gdt << 3 porque los ultimos 3 bits son reservados
 * 
 * NULLO           selector[0] =   0000b = 0x 
 * FORK_TASK       selector[1] =   1000b = 0x8 
 * VIDEO           selector[2] =  10000b = 0x10
 * TAREA_INICIAL   selector[3] =  11000b = 0x18; 
 * DATOS           selector[4] = 100000b = 0x20; 
 * TAREA_IDLE      selector[5] = 101000b = 0x28; 
 * CODIGO          selector[6] = 110000b = 0x30; 
 ************************************************************/

gdt_entry gdt[GDT_COUNT] = {
	/* Descriptor NULO*/
	(gdt_entry){(unsigned int) 0x00000000, (unsigned int) 0x00000000 },
    (gdt_entry){(unsigned int) 0x00000000, (unsigned int) 0x00000000 },

    /* Descriptor VIDEO posicion 2*/
	(gdt_entry){.base_0_15 = 0x8000,   // base = 0xB8000
                .base_23_16 = 0xB,
                .limit_0_15 = 0xF9F,   // limit = 80x25x2 = 4000 -1 = F9F
                .type = 0x2,           // type = R/W
                .s = 0x1,              // s = datos
                .p = 0x1,              // p = presente
                .db = 0x1,             // operation_size = 32b
                .g = 0,                 // granularity =  bytes
                .dpl = 0,
                .limit_16_19 = 0,
                .avl = 0,
                .l = 0,
                .base_31_24 = 0
    },
    /* Descriptor TAREA_INCIAL posicion 3*/
    (gdt_entry){(unsigned int) 0x00000000, (unsigned int) 0x00000000 },

    /* Descriptor DATOS posicion 4*/
	(gdt_entry){                        // base = 0x00000
                .limit_0_15 = 0xFFFF,  //limite = TODO = FFFFF 
                .limit_16_19 = 0xF,
                .type = 0x2,           // type = R/W
                .s = 0x1,              // s = datos
                .p = 0x1,              // p = presente
                .db = 0x1,             // operation_size = 32b
                .g = 1,                 // granularity =  4Kb (pages)
                .dpl = 0,
                .avl = 0,
                .l = 0,
                .base_31_24 = 0,
                .base_0_15 = 0,
                .base_23_16 = 0
    },	

    /* Descriptor TAREA_IDLE posicion 5*/
    (gdt_entry){(unsigned int) 0x00000000, (unsigned int) 0x00000000 },

    /* Descriptor CODIGO posicion 6*/
	(gdt_entry){                        // base = 0x00000
                .limit_0_15 = 0xFFFF,  //limite = TODO = FFFFF 
                .limit_16_19 = 0xF,
                .type = 0xA,           // type = Ex/Read
                .s = 0x1,              // s = datos
                .p = 0x1,              // p = presente
                .db = 0x1,             // operation_size = 32b
                .g = 1,                 // granularity =  4Kb (pages)
                .dpl = 0,
                .avl = 0,
                .l = 0,
                .base_31_24 = 0,
                .base_0_15 = 0,
                .base_23_16 = 0
    },
};

gdt_descriptor GDT_DESC = {sizeof(gdt)-1, (unsigned int)&gdt};
